﻿ using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading.Tasks;
using IDAL;
using MODEL;

namespace DAL
{
    public class DBContextFactory : IDBContextFactory
    {
        #region  创建 EF上下文 对象，在线程中共享 一个 上下文对象  +DbContext GetDbContext()
        public DbContext GetDbContext(string type)
        {
            DbContext DBContext = CallContext.GetData(type) as DbContext;
            if (DBContext == null)
            {
                switch (type)
                {
          
                case "AreaCarriersDeliveryMethod":DBContext = new ADSystemEntities();break;
          
                case "AreaCarriersDeliveryMethodChargeType":DBContext = new ADSystemEntities();break;
          
                case "AreaCarriersDeliveryMethodContraband":DBContext = new ADSystemEntities();break;
          
                case "AreaCarriersDeliveryMethodPackage":DBContext = new ADSystemEntities();break;
          
                case "AuthorizedInfoConfig":DBContext = new ADSystemEntities();break;
          
                case "Carrier":DBContext = new ADSystemEntities();break;
          
                case "Category":DBContext = new ADSystemEntities();break;
          
                case "CategorySpecific":DBContext = new ADSystemEntities();break;
          
                case "Contraband":DBContext = new ADSystemEntities();break;
          
                case "Currency":DBContext = new ADSystemEntities();break;
          
                case "CurrencyConvertLoss":DBContext = new ADSystemEntities();break;
          
                case "CustomsRateInfo":DBContext = new ADSystemEntities();break;
          
                case "DeliveryMethod":DBContext = new ADSystemEntities();break;
          
                case "DistributeArea":DBContext = new ADSystemEntities();break;
          
                case "DistributeAreaStock":DBContext = new ADSystemEntities();break;
          
                case "GrossProfitConfig":DBContext = new ADSystemEntities();break;
          
                case "LanguageType":DBContext = new ADSystemEntities();break;
          
                case "ListingFeeInfo":DBContext = new ADSystemEntities();break;
          
                case "LogingAccountInfo":DBContext = new ADSystemEntities();break;
          
                case "LogingSellerAccountInfo":DBContext = new ADSystemEntities();break;
          
                case "LogisticsCountry":DBContext = new ADSystemEntities();break;
          
                case "LogisticsInfoConfig":DBContext = new ADSystemEntities();break;
          
                case "Measure":DBContext = new ADSystemEntities();break;
          
                case "PaypalFee":DBContext = new ADSystemEntities();break;
          
                case "ProductAd":DBContext = new ADSystemEntities();break;
          
                case "ProductCategory":DBContext = new ADSystemEntities();break;
          
                case "ProductConfig":DBContext = new ADSystemEntities();break;
          
                case "ProductCostInfo":DBContext = new ADSystemEntities();break;
          
                case "ProductInfo":DBContext = new ADSystemEntities();break;
          
                case "ProductPackageInfo":DBContext = new ADSystemEntities();break;
          
                case "ProductPicture":DBContext = new ADSystemEntities();break;
          
                case "ProductProperty":DBContext = new ADSystemEntities();break;
          
                case "ProductSpec":DBContext = new ADSystemEntities();break;
          
                case "ProductStatu":DBContext = new ADSystemEntities();break;
          
                case "ProductTranslation":DBContext = new ADSystemEntities();break;
          
                case "RateOfExchange":DBContext = new ADSystemEntities();break;
          
                case "RateOfExchangeAnalysi":DBContext = new ADSystemEntities();break;
          
                case "RateOfExchangeHistory":DBContext = new ADSystemEntities();break;
          
                case "SalesFinalFee":DBContext = new ADSystemEntities();break;
          
                case "SalesFinalFeeSection":DBContext = new ADSystemEntities();break;
          
                case "SalesPlatform":DBContext = new ADSystemEntities();break;
          
                case "SalesPlatformSiteCurrency":DBContext = new ADSystemEntities();break;
          
                case "SalesPlatformSiteInfo":DBContext = new ADSystemEntities();break;
          
                case "SalesPlatformSiteInfoCategory":DBContext = new ADSystemEntities();break;
          
                case "SalesPlatformSiteInfoCategorySpecific":DBContext = new ADSystemEntities();break;
          
                case "SalesPlatformSiteInfoLanguageType":DBContext = new ADSystemEntities();break;
          
                case "SalesPlatformSiteSellerAccountInfo":DBContext = new ADSystemEntities();break;
          
                case "SalesPlatformSiteSellerAccountInfoTemplateWebPageTemplateTerm":DBContext = new ADSystemEntities();break;
          
                case "SellerAccountInfo":DBContext = new ADSystemEntities();break;
          
                case "SiteInfo":DBContext = new ADSystemEntities();break;
          
                case "SiteSpecRelation":DBContext = new ADSystemEntities();break;
          
                case "Specific":DBContext = new ADSystemEntities();break;
          
                case "SpecificsValue":DBContext = new ADSystemEntities();break;
          
                case "Stock":DBContext = new ADSystemEntities();break;
          
                case "SystemMenu":DBContext = new ADSystemEntities();break;
          
                case "TemplateTerm":DBContext = new ADSystemEntities();break;
          
                case "TemplateTermsDetail":DBContext = new ADSystemEntities();break;
          
                case "TemplateWebPage":DBContext = new ADSystemEntities();break;
          
                case "TransportCharge":DBContext = new ADSystemEntities();break;
          
                case "TransportType":DBContext = new ADSystemEntities();break;
          
                case "Value":DBContext = new ADSystemEntities();break;
          
                case "VATFeeInfo":DBContext = new ADSystemEntities();break;
default : DBContext = new ADSystemEntities(); break;
                }
                DBContext.Configuration.ValidateOnSaveEnabled = false;
                CallContext.SetData(type, DBContext);
            }
            return DBContext;
        } 
        #endregion
    }

    public class DBSessionFactory:IDBSessionFactory
    {
        #region 在线程中 共用一个 DBSession 对象
        /// <summary>
        /// 在线程中 共用一个 DBSession 对象
        /// </summary>
        /// <returns></returns>
        public IDBSession GetDBSession()
        {
            //从当前线程中  获得DBContext    数据仓储对象
            IDBSession DBSession = CallContext.GetData(typeof(DBSessionFactory).Name) as DBSession;
            if (DBSession==null)
            {
                DBSession = new DBSession();
                CallContext.SetData(typeof(DBSessionFactory).Name, DBSession);
            }
            return DBSession;
        }
        #endregion
    }
}